<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * CodeIgniter PHPMAILER Plugin
 *
 * @package		CodeIgniter
 * @subpackage	Plugin
 * @category	Plugin
 */

// ------------------------------------------------------------------------

/**
 * Send SMTP Email
 *
 * @access	public
 * @param 	string - email
 * @param 	string - name
 * @param 	string
 * @param 	string
 * @param 	array
 * @return	bool
 * table name : TBL_DB_EMAIL_LOG
 */	
function send_smtp_email($recipient_email, $recipient_name, $subject, $message='NA', $param_arr=array(), $fax = false)
{
	$CI = & get_instance();
  
  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

  // Additional headers
  //$from = "someonelse@serialslink.com";
  $headers .= 'To: '. $recipient_name . " <".$recipient_email."> \r\n";
  $headers .= 'From: '.OS_CRM_FROM_NAME.' <'.OS_CRM_FROM_EMAIL.'>' . "\r\n";
  $headers .= 'Reply-To: '. OS_CRM_ADMIN_EMAIL . "\r\n" ;
  //$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
  $headers .= 'Bcc: '. OS_CRM_BCC_EMAIL . "\r\n";

  // Mail it
  if (SMTP_SEND_MAIL) {
    $send_status = @mail($recipient_email, $subject, $message, $headers);
  } 
    

  $usr_id = 'Guest';
  $usr_info = __loggedin_info('usr_info');
  if(isset ($usr_info['usr_id'])){
    $usr_id = $usr_info['usr_id'];
  }
  $parent_controller = $CI->session->CI->router->class;
  $extra_info = base64_encode(serialize($param_arr));
  $data_arr = array(
      'recp_name' => $recipient_email,
      'recp_email' => $recipient_name,
      'message' => $message,
      'email_subject' => $subject,
      'extra_info' => $headers,
      'status' => $send_status,
      'module' => $parent_controller,
      'created' => date('Y-m-d H:i:s'),
      'created_by' => $usr_id
  );

  if (!$send_status) {
    //echo 'Failed to Send';
    $CI->db->insert(TBL_DB_EMAIL_LOG, $data_arr);
    return 0;
  } else {
    //echo 'Mail Sent';
    $CI->db->insert(TBL_DB_EMAIL_LOG, $data_arr);
    return 1;
  }
}


/**
 * Send/Queue SMTP Email generated from CRONS
 *
 * @access	public
 * @param 	string - email
 * @param 	string - name
 * @param 	string - subject
 * @param 	string - body
 * @param 	array  - extra params
 * @param	bool   - to send email/store email
 * @return	bool
 * table name : TBL_CRON_EMAIL_LOG
 * @author	Dhaval
 */	
function send_queue_cron_smtp_email($recipient_email, $recipient_name, $subject, $message='NA', $param_arr=array(), $fax_status = false)
{
	$CI =& get_instance(); 
	// By default this will be set by the system config
	$bcc 		= SMTP_BCC;
	$reply_to 	= SMTP_REPLY_TO;
	
	//Check of the BCC flag is overwritten and set it accordingly
	if(isset($param_arr['bcc_flag']))
		$bcc 		= $param_arr['bcc_flag'];
	
	//Check of the Reply To flag is overwritten	and set it accordingly
	if(isset($param_arr['reply_to_flag']))
		$reply_to	= $param_arr['reply_to_flag'];	
	
	$attach_path = '';
	$attach_name = '';
	$attach_files = array();
	
	if(isset($param_arr['attach_path']))
		$attach_path = $param_arr['attach_path'];
	
	if(isset($param_arr['attach_name']))
		$attach_name = $param_arr['attach_name']; 
		
	if(isset($param_arr['attach_files']))
		$attach_files = $param_arr['attach_files']; 
		
	if(isset($param_arr['from_name']) && $param_arr['from_name'] != '' && !empty($param_arr['from_name']))
	{
		$from_name = $param_arr['from_name'];
	}
	else
	{
		$from_name = SMTP_ADMIN_NAME;
	}
	if(isset($param_arr['from_email']) && $param_arr['from_email'] != '' && !empty($param_arr['from_email']))
	{
		$from_email 	= $param_arr['from_email'];
	}else
	{
		$from_email 	= SMTP_ADMIN_EMAIL;
	}
		
		
	
	$parent_controller = $CI->session->CI->router->class;
	$extra_info = base64_encode(serialize($param_arr));
	$data_arr = array(
					'recp_name'				=> $recipient_name,
					'recp_email'			=> $recipient_email,
					'from_name'				=> $from_name,
					'from_email'			=> $from_email,
					'cc_email'				=> $bcc,
					'fax'					=> ($fax_status) ? 1:0,
					'email_message'			=> $message,
					'email_subject'			=> $subject,
					'email_attachment'		=> $attach_path,
					'email_attachment_name' => $attach_name,
					'email_attachment_files'=> serialize($attach_files),
					'extra_info'			=> $extra_info,
					'module'				=> $parent_controller,
					'flag'					=> 0,
					'created' 				=> date('Y-m-d H:i:s'),
				);
	
	$CI->db->insert('cron_email_log', $data_arr); 
		
	return 1;
}

// ------------------------------------------------------------------------

/**
 * Send SMTP Email To Admin
 *
 * @access	public
 * @param 	string
 * @param 	string
 * @param 	array
 * @return	bool  
 */	
function send_smtp_email_admin($subject, $message='NA', $param_arr=array())
{
	$CI = & get_instance();

  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

  // Additional headers
  //$from = "someonelse@serialslink.com";
  $headers .= 'To: '. OS_CRM_ADMIN_EMAIL . " \r\n";
  $headers .= 'Reply-To: '. OS_CRM_ADMIN_EMAIL . "\r\n" ;
  $headers .= 'From: '.OS_CRM_FROM_NAME.' <'.OS_CRM_FROM_EMAIL.'>' . "\r\n";
  
  // Mail it
  if (SMTP_SEND_MAIL) {
    $send_status = @mail(OS_CRM_ADMIN_EMAIL, $subject, $message, $headers);
  } 
  
  if (!$send_status) {
    //echo 'Failed to Send';		
    return 0;
  } else {
    //echo 'Mail Sent';
    return 1;
  }
}
?>